package com.ruoyi.fire.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.ResponseEntityInfo;
import com.ruoyi.common.enums.RequestMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.RequestService;
import com.ruoyi.fire.domain.EBuildingFireHazard;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.fire.mapper.EBuildingTypeMapper;
import com.ruoyi.fire.domain.EBuildingType;
import com.ruoyi.fire.service.IEBuildingTypeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 建筑物类型Service业务层处理
 *
 * @author ruoyi
 * @date 2025-11-02
 */
@Service
@RequiredArgsConstructor
public class EBuildingTypeServiceImpl extends ServiceImpl<EBuildingTypeMapper, EBuildingType> implements IEBuildingTypeService {
    @Autowired
    private EBuildingTypeMapper eBuildingTypeMapper;

    private final RequestService requestService;

    /**
     * 查询建筑物类型
     *
     * @param id 建筑物类型主键
     * @return 建筑物类型
     */
    @Override
    public EBuildingType selectEBuildingTypeById(Long id) {
        return eBuildingTypeMapper.selectEBuildingTypeById(id);
    }

    /**
     * 查询建筑物类型列表
     *
     * @param eBuildingType 建筑物类型
     * @return 建筑物类型
     */
    @Override
    public List<EBuildingType> selectEBuildingTypeList(EBuildingType eBuildingType) {
        return eBuildingTypeMapper.selectEBuildingTypeList(eBuildingType);
    }

    /**
     * 新增建筑物类型
     *
     * @param eBuildingType 建筑物类型
     * @return 结果
     */
    @Override
    public int insertEBuildingType(EBuildingType eBuildingType) {
        return eBuildingTypeMapper.insertEBuildingType(eBuildingType);
    }

    /**
     * 修改建筑物类型
     *
     * @param eBuildingType 建筑物类型
     * @return 结果
     */
    @Override
    public int updateEBuildingType(EBuildingType eBuildingType) {
        eBuildingType.setUpdateTime(DateUtils.getNowDate());
        return eBuildingTypeMapper.updateEBuildingType(eBuildingType);
    }

    /**
     * 批量删除建筑物类型
     *
     * @param ids 需要删除的建筑物类型主键
     * @return 结果
     */
    @Override
    public int deleteEBuildingTypeByIds(Long[] ids) {
        return eBuildingTypeMapper.deleteEBuildingTypeByIds(ids);
    }

    /**
     * 删除建筑物类型信息
     *
     * @param id 建筑物类型主键
     * @return 结果
     */
    @Override
    public int deleteEBuildingTypeById(Long id) {
        return eBuildingTypeMapper.deleteEBuildingTypeById(id);
    }

    /**
     * 数据同步
     *
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void dataSync() {
        try {
            ResponseEntityInfo responseEntityInfo = requestService.advancedRequest(RequestMapper.BUILDING_TYPE, null);
            if (responseEntityInfo != null && responseEntityInfo.getErrno() == 0) {
                Object data = responseEntityInfo.getData();
                if (data == null) {
                    // 记录警告日志：返回数据为空
                    return;
                }

                // 使用条件删除替代全表删除，绕过保护机制
                getBaseMapper().delete(new LambdaQueryWrapper<EBuildingType>().isNotNull(EBuildingType::getId));
                List<EBuildingType> eEconomicTypes = JSON.parseArray(JSON.toJSONString(data), EBuildingType.class);
                if (eEconomicTypes != null && !eEconomicTypes.isEmpty()) {
                    // 设置更新时间
                    eEconomicTypes.forEach(item -> item.setUpdateTime(DateUtils.getNowDate()));
                    saveBatch(eEconomicTypes);
                }
            }
        } catch (Exception e) {
            // 记录错误日志
            throw new RuntimeException("数据同步过程中发生异常", e);
        }
    }
}
